#ifndef MULTITHREAD_H
#define MULTITHREAD_H

#include <QObject>
#include <QThread>

class MultiThread : public QObject
{
    Q_OBJECT
public:
    explicit MultiThread(QObject *parent = nullptr);
    static void stop(void)
    {
        _stop=1;
    }
    static void start(void)
    {
        _stop=0;
    }
    static uint8_t is_run(void)
    {
        return (0==_stop);
    }

signals:
    void resultReady(const QString result, const void* data);

public slots:
    virtual void doWork(const QString parameter)=0;

private:
    static uint8_t _stop;

};

class MultiThread_Test1 : public MultiThread
{
    Q_OBJECT
public:
    explicit MultiThread_Test1(QObject *parent = nullptr) : MultiThread(parent)
    {
        ;
    }

public slots:
    virtual void doWork(const QString parameter) override;

};
class MultiThread_Test2 : public MultiThread
{
    Q_OBJECT
public:
    explicit MultiThread_Test2(QObject *parent = nullptr) : MultiThread(parent)
    {
        ;
    }

public slots:
    virtual void doWork(const QString parameter) override;

};


class ControllerMultiThread : public QObject
{
    Q_OBJECT
    QThread workerThread;
public:
    ControllerMultiThread(MultiThread *work) {
        //Worker *worker = new Worker;
        worker = work; // new MultiThread();
        worker->moveToThread(&workerThread);
        connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
#if 0
        connect(this, &ControllerMultiThread::operate, worker, &MultiThread::doWork);
        connect(worker, &MultiThread::resultReady, this, &ControllerMultiThread::handleResults);
#else
        connect(this, SIGNAL(operate(const QString)), worker, SLOT(doWork(const QString)));
        //connect(worker, SIGNAL(resultReady()), this, SLOT(handleResults()));
        //connect(this, &ControllerMultiThread::operate, worker, &MultiThread::doWork);
#endif
        workerThread.start();
    }
    ~ControllerMultiThread() {
        workerThread.quit();
        workerThread.wait();
    }

    void start(const QString &parameter)
    {
        worker->start();
        emit operate(parameter);
    }

//public slots:
//    void handleResults(const QString &)
//    {
//        ;
//    }
signals:
    void operate(const QString);

private:
    MultiThread *worker;

};

extern void MultiThreadTest_Start(void);
extern void MultiThreadTest_Stop(void);

#endif // MULTITHREAD_H
